/*
 *    Copyright 2011-2012 The Hook Team
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.hook.yaksha.config;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 配置抽象类
 * 
 * 参数采取动态获取，每次获取都会可能产生消耗，注意使用场景
 * 
 * @author suwuqiang 2011-12-26
 */
public class AbstractConfig implements Config {
	private static final long serialVersionUID = -7045942569164275313L;
	private final Log logger = LogFactory.getLog(AbstractConfig.class);
	Map<String, String> properties = new HashMap<String, String>();

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.hook.yaksha.config.Config#addAllToConfig(java.util.Map)
	 */
	@Override
	public void addAllToConfig(Map<String, String> props) {
		this.properties.putAll(props);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.hook.yaksha.config.Config#load(java.lang.String)
	 */
	@Override
	public void load(String propertiesFile) {
		InputStream in = null;
		try {
			// TODO in = ReportUtil.getInputStreamFromFile(propertiesFile);
			Properties prop = new Properties();
			prop.load(in);
			Iterator<Object> keys = prop.keySet().iterator();
			while (keys.hasNext()) {
				String key = (String) keys.next();
				properties.put(key, prop.getProperty(key));
			}
		} catch (Exception ex) {
			logger.error(ex, ex);
		} finally {
			if (in != null)
				try {
					in.close();
				} catch (IOException e) {
					logger.error(e, e);
				}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.hook.yaksha.config.Config#get(java.lang.String)
	 */
	@Override
	public String get(String propName) {
		return this.properties.get(propName);
	}
}
